/**
 * Graph.java
 * colourmap
 * Copyright (C) 2010 Alan W.F. Boyd
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact the author via the google code web page for the project
 * <http://colourmap.googlecode.com>
 */
package com.googlecode.colourmap;

import java.awt.Graphics;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.ProgressMonitor;

import com.googlecode.colourmap.colours.IColourScheme;


/**
 * The Graph class takes care of painting the colour map.
 * 
 * @author Alan W.F. Boyd
 */
public class Graph extends JFrame
{
	/** The size of the border of the JFrame */
	public static final int BORDER = 25;
	
	/** The size of the title bar of the JFrame */
	public static final int TITLEBAR = 20;
	
	/** The serial number of this version of the class */
	private static final long serialVersionUID = 4915029103060593179L;

	/** The data of the colour map */
	private GraphData data;
	
	/** The colour scheme that takes care of dictating which colours to use and where */
	private IColourScheme scheme;
	
	/** Progress monitor for application tasks. Manages progress bars */
	private ProgressMonitor monitor;
	
	/**
	 * Creates a graph with a particular title and using a data file
	 * @param title
	 * @param d
	 */
	public Graph(String title, GraphData d) 
	{
		super(title);
		this.data = d;
		this.setSize(data.getGraphWidth(), data.getGraphHeight());
	}
	
	/**
	 * Specifies the colour scheme that should be used in the graph.
	 * The colour scheme maps data values to particular colours
	 * @param s the colour scheme
	 */
	public void setColourScheme(IColourScheme s)
	{
		scheme = s;
		scheme.setMax(data.getMaxPoint());
		scheme.setMin(data.getMinPoint());
	}
	
	/**
	 * Sets the monitor object that tracks the application progress
	 * @param m the new ProgressMonitor object
	 */
	public void setMonitor(ProgressMonitor m)
	{
		monitor = m;
	}
	
	/**
	 * Takes care of painting the colour map graph
	 * @param gfx the graphics context
	 */
	public void paint(Graphics gfx)
	{
		gfx.clearRect(0, 0, data.getGraphWidth(), data.getGraphHeight()); 

		float minX = data.getMinX();
		float minY = data.getMinY();
		
		Iterator<Point> i = data.getAllDataPoints().keySet().iterator();
		while (i.hasNext())
		{
			Point c = i.next();
				
			if (scheme != null)
				gfx.setColor(scheme.getColour(data.getAllDataPoints().get(c)));
			gfx.fillRect((int)(c.x-minX+BORDER), (int)(c.y-minY+BORDER+TITLEBAR), data.getResolution(), data.getResolution());
		}
		
		i = data.getOriginalDataPoints().keySet().iterator();
		while (i.hasNext())
		{
			Point c = i.next();
			
			if (scheme != null)
				gfx.setColor(scheme.getOutlineColour());
			gfx.drawRect((int)(c.x-minX+BORDER), (int)(c.y-minY+BORDER+TITLEBAR), data.getResolution(), data.getResolution());
		}
		
		if (monitor != null)
			monitor.setProgress(100);
	}
}
